package com.yqg.protection.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yqg.protection.entity.Organization;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author zxf
 * @since 2024-01-01
 */
@Mapper
public interface OrganizationMapper extends BaseMapper<Organization> {

    @Select("SELECT T2.*\n" +
            "FROM ( \n" +
            "    SELECT \n" +
            "        @r AS _id, \n" +
            "        (SELECT @r := pid FROM organization WHERE id = _id) AS pid, \n" +
            "        @l := @l + 1 AS lvl \n" +
            "    FROM \n" +
            "        (SELECT @r := #{organizationId}, @l := 0) vars, \n" +
            "        organization h \n" +
            "    WHERE @r <> 0) T1 \n" +
            "JOIN organization T2 \n" +
            "ON T1._id = T2.id \n" +
            "ORDER BY T1.lvl DESC")
    Map<Long, Long> getPIdList(Long organizationId);

    @Select("")
    List<Long> getCIdList(Long organizationId);
}
